<?php

class My_Object {

    function  __construct($id = null) {
        $this->tableName = 'objects';
        $this->table = new Zend_Db_Table($this->tableName);

        $info = $this->table->info();
        $this->primary = $info['primary'][1];

        $this->request = new Zend_Controller_Request_Http;

        if(is_array($id)){
            $id = $id[$this->primary];
        }
        if(!empty($id)){
            $this->setId($id);
            $this->readById();
        }
    }

    function setId($id){
        $this->id = $id;
    }

    function getId(){
        return $this->id;
    }

    function exists(){
        return $this->data[$this->primary] ? true : false;
    }

    function readById($id = null){
        if(!empty($id)){
            $this->setId($id);
        }
        $result = $this->table->find($this->getId());
        $data = $result->toArray();
        $this->data = $data[0];
    }

    function getAll(){
        $result = $this->table->fetchAll(
            $this->table->select()
                ->order('id')
            );
        return $result->toArray();
    }

    function getName($id = null){
        if(!empty($id)){
            $this->setId($id);
        }
        if(!$this->exists()){
            $this->readById();
        }
        return $this->data['name'];
    }

    function getData($id = null){
        if(!empty($id)){
            $this->setId($id);
        }
        if(!$this->exists()){
            $this->readById();
        }
        if(!is_array($this->data['data'])){
            $this->data['data'] = unserialize($this->data['data']);
        }
        return $this->data['data'];
    }

    function getPluginClassNames($id = null){
        if(!empty($id)){
            $this->setId($id);
        }
        if(!$this->exists()){
            $this->readById();
        }

        $data = $this->getData();
        foreach($data['plugins'] AS $plugin){
            $i++;
            $$plugin = str_replace('plugin_', '', $plugin['name']);
            $name = explode('_', $$plugin);
            $pluginName = null;
            if(is_array($name)){
                foreach($name AS $word){
                    $pluginName .= ucfirst(trim($word));
                }
            }else{
                $pluginName .= ucfirst($word);
            }

            $pluginNameReturn[] = $pluginName;
        }
        return $pluginNameReturn;
    }

}
?>
